// Copyright (C) <2018> Zealcomm Inc.
//
// SPDX-License-Identifier: Apache-2.0
#import <Foundation/Foundation.h>
#import <WebRTC/RTCMacros.h>
#import <WebRTC/RTCLegacyStatsReport.h>
#import "IRTC/IRTCLocalStream.h"
#import "IRTC/IRTCP2PClientConfiguration.h"
#import "IRTC/IRTCP2PPeerConnectionChannelObserver.h"
#import "IRTC/IRTCP2PSignalingChannelProtocol.h"
#import "IRTC/IRTCP2PSignalingSenderProtocol.h"
NS_ASSUME_NONNULL_BEGIN
@protocol IRTCP2PClientDelegate;
@class IRTCP2PPublication;
/// An async client for P2P WebRTC sessions<br>一个用于P2P WebRTC会话的异步客户端。
RTC_EXPORT
@interface IRTCP2PClient : NSObject<IRTCP2PPeerConnectionChannelObserver,
                                   IRTCP2PSignalingSenderProtocol,
                                   IRTCP2PSignalingChannelDelegate>
/**
 @brief Initialize a IRTCP2PClient instance with a specific signaling channel.<br>用指定信令通道来初始化一个IRTCP2PClient实例。
 @param configuration Configuration for creating the IRTCP2PClient.<br>创建IRTCP2PClient的配置参数。
 @param signalingChannel Signaling channel used for exchange signaling messages.<br>用于信令交互的信令通道。
 */
- (instancetype)initWithConfiguration:(IRTCP2PClientConfiguration*)configuration
                     signalingChannel:
                         (id<IRTCP2PSignalingChannelProtocol>)signalingChannel;
/**
 @brief Connect to the signaling server.<br>连接到信令服务器。
 @param token A token used for connection and authentication<br>用于连接和认证的令牌。
 @param onSuccess Success callback will be invoked with current user's ID if
 connect to server successfully.<br>如果成功连接到服务器，成功回调会返回当前用户ID
 @param onFailure Failure callback will be invoked if one of these cases
 happened:<br>如果下列事件之一发生则会触发错误回调：
                1. IRTCP2PClient is connecting or connected to a server.<br>IRTCP2PClient正在连接服务器或者连接失败。
                2. Invalid token.<br>无效的令牌。
 */
- (void)connect:(NSString*)token
      onSuccess:(nullable void (^)(NSString*))onSuccess
      onFailure:(nullable void (^)(NSError*))onFailure;
/**
 @brief Disconnect from the signaling server.<br>从信令服务器断开连接。
 It will stop all active WebRTC sessions.<br>这将停止所有活动的WebRTC会话。
 @param onSuccess Success callback will be invoked if disconnect from server
 successfully.
 @param onFailure Failure callback will be invoked if one of these cases
 happened:<br>如果下列事件之一发生则会触发错误回调：
                1. IRTCP2PClient haven't connected to a signaling server.<br>IRTCP2PClient没有连接到信令服务器。
 */
- (void)disconnectWithOnSuccess:(nullable void (^)())onSuccess
                      onFailure:(nullable void (^)(NSError*))onFailure;
/**
 @brief Send a message to remote client<br>发送消息给远端客户端。
 @param message The message to be sent.<br>要发送的消息。
 @param targetId Remote user's ID.<br>远端用户的ID。
 @param onSuccess Success callback will be invoked if send deny event
 successfully.<br>如果发送消息成功会触发成功回调。
 @param onFailure Failure callback will be invoked if one of the following cases
 happened.<br>如果下列事件之一发生则会触发错误回调：
 1. IRTCP2PClient is disconnected from the server.<br>IRTCP2PClient从服务器断开。
 2. Target ID is nil or target user is offline.<br>目标ID为空或者目标用户不在线。
 3. There is no WebRTC session with target user.<br>和目标用户没有WebRTC会话连接。
 */
- (void)send:(NSString*)message
           to:(NSString*)targetId
    onSuccess:(nullable void (^)())onSuccess
    onFailure:(nullable void (^)(NSError*))onFailure;
/**
 @brief Stop a WebRTC session.<br>停止一个WebRTC会话连接。
 @details Clean all resources associated with given remote endpoint. It may include RTCPeerConnection,
 RTCRtpTransceiver and RTCDataChannel. It still possible to publish a stream, or send a message to
 given remote endpoint after stop.<br>清除所有和指定远端相关的资源。可能包括RTCPeerConnection，
  RTCRtpTransceiver和RTCDataChannel。当停止之后，还是可能对指定远端发布流或发送消息。
 @param targetId Remote user's ID.<br>远端用户的ID。
 */
- (void)stop:(NSString*)targetId;
/**
 @brief Publish a stream to the remote client.<br>发布一条流到远端客户端。
 @param stream The stream which will be published.<br>要发布的媒体流。
 @param to Target user's ID.<br>目标用户ID。
 @param onSuccess Success callback will be invoked it the stream is published.<br>如果流发布成功则触发成功回调。
 @param onFailure Failure callback will be invoked if one of these cases
 happened:<br>如果下列事件之一发生则会触发错误回调：
                  1. IRTCP2PClient is disconnected from server.<br>IRTCP2PClient从服务器断开。
                  2. Target ID is nil or user is offline.<br>目标ID为空或者目标用户不在线。
                  3. Haven't connected to remote client.<br>没有和远端客户端连接。
 */
- (void)publish:(IRTCLocalStream*)stream
             to:(NSString*)targetId
      onSuccess:(nullable void (^)(IRTCP2PPublication*))onSuccess
      onFailure:(nullable void (^)(NSError*))onFailure;
/**
 @brief Get the connection statistowt with target client.<br>获取目标客户端的连接统计信息。
 @param targetId Remote user's ID.<br>远端用户的ID。
 @param onSuccess Success callback will be invoked if get statistoms
 information successes.<br>如果成功获取统计信息则触发成功回调。
 @param onFailure Failure callback will be invoked if one of the following
 cases happened.<br>如果下列事件之一发生则会触发错误回调：
 1. Target ID is invalid.<br>目标ID无效。
 2. There is no WebRTC session with target user.<br>没有和目标用户有WebRTC会话连接。
 */
- (void)statsFor:(NSString*)targetId
       onSuccess:(void (^)(NSArray<RTCLegacyStatsReport*>*))onSuccess
       onFailure:(nullable void (^)(NSError*))onFailure;
@property(nonatomic, weak) id<IRTCP2PClientDelegate> delegate;
@property(nonatomic, strong) NSMutableArray<NSString*>* allowedRemoteIds;
@end
/// Delegate for IRTCConferenceClient.<br>IRTCConferenceClient代理。
RTC_EXPORT
@protocol IRTCP2PClientDelegate<NSObject>
@optional
/**
  @brief Triggered when client is disconnected from signaling server.<br>当客户端从信令服务器断开连接时触发。
*/
- (void)p2pClientDidDisconnect:(IRTCP2PClient*)client;
/**
  @brief Triggered when a stream is added.<br>当新的媒体流加入时触发。
  @param stream The stream which is added.<br>新加入的媒体流。
*/
- (void)p2pClient:(IRTCP2PClient*)client didAddStream:(IRTCRemoteStream*)stream;
/**
  @brief Triggered when a message is received.<br>当收到新的消息时触发。
  @param senderId Sender's ID.<br>发生者的ID。
  @param message Message received.<br>收到的消息。
*/
- (void)p2pClient:(IRTCP2PClient*)client
    didReceiveMessage:(NSString*)message
                 from:(NSString*)senderId;
@end
NS_ASSUME_NONNULL_END
